home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / comm2 / alist.lha / src / alcmd.e < prev    next >
Text File  |  1995-11-08  |  10KB  |  361 lines

  1. /* ALCmd.M */
  2.  
  3. OPT MODULE
  4. OPT EXPORT
  5.  
  6. /* Module to process commands to the AList mailing list server */
  7.  
  8. MODULE 'other/alconfig'
  9. MODULE 'other/aladd'
  10. MODULE 'other/alpost'
  11.  
  12.  
  13. DEF config_list:PTR TO config_node, hostname:PTR TO CHAR
  14.  
  15. /*
  16.  * Perform one or more commands, as specified by the chained string 'str'.
  17.  */
  18. PROC do_command (str:PTR TO CHAR)
  19.    DEF from:PTR TO CHAR, to:PTR TO CHAR, list:PTR TO config_node, tmp:PTR TO config_node, s:PTR TO CHAR
  20.    DEF s2:PTR TO CHAR, x, y
  21.  
  22.    to := str
  23.    list := find_list (to)
  24.    from := str := Next (str)
  25.    str := Next (str)
  26.  
  27.    WHILE (is_empty (str) = FALSE)
  28.       str := Next (str)
  29.    ENDWHILE
  30.  
  31.    IF (str = NIL)  THEN RETURN      /* Don't do a Next (NIL)! */
  32.  
  33.    clear_tmp_file()
  34.    s := String (78)
  35.    WHILE (str := Next (str))
  36.       IF (StrCmp (str, 'HELP', 4))
  37.          add_tmp_file ('\nResponse to command\n>>')
  38.          add_tmp_file (str)
  39.          add_tmp_file ('\n')
  40.          add_tmp_help (TrimStr (str+4))
  41.       ELSEIF (StrCmp (str, 'INDEX', 5))
  42.          add_tmp_file ('\nResponse to command\n>>INDEX\n\n')
  43.          tmp := config_list
  44.          IF (tmp)  THEN add_tmp_file ('Visible lists:')  ELSE add_tmp_file ('No visible lists.\n\n')
  45.          WHILE (tmp)
  46.             StringF (s, '\n\s[15]\t  \s\n', tmp.name, IF (tmp.sdesc)  THEN tmp.sdesc ELSE '')
  47.             add_tmp_file (s)
  48.             tmp := tmp.next
  49.          ENDWHILE
  50.       ELSEIF (StrCmp (str, 'LIST', 4))
  51.          add_tmp_file ('\nResponse to command\n>>')
  52.          add_tmp_file (str)
  53.  
  54.          StrCopy (s, from)
  55.          s2 := TrimStr (str+4)
  56.          y := 0
  57.          IF (IF (s2 = -1)  THEN FALSE ELSE (s2[0] > 32))
  58.             StrCopy (s, s2, StrLen (s2) - 1)
  59.             y := 1
  60.          ENDIF
  61.  
  62.          tmp := config_list;  x := 0
  63.          WHILE (tmp)
  64.             IF ((y = 0) OR ((tmp.flags AND CNF_INVISIBLE) = 0) )
  65.                IF (on_list (s, tmp.users, (tmp.flags AND CNF_CMP_DOMAIN)))
  66.                   x := 1
  67.                   StringF (s, '\s[15]\t  \s\n', tmp.name, IF (tmp.sdesc)  THEN tmp.sdesc ELSE '')
  68.                   add_tmp_file (s)
  69.                ENDIF
  70.             ENDIF
  71.             tmp := tmp.next
  72.          ENDWHILE
  73.  
  74.          IF (x = 0)
  75.             IF (y = 0)
  76.                add_tmp_file ('You are not on any lists at this site.\nYour address is "')
  77.                add_tmp_file (s)
  78.                add_tmp_file ('".\n')
  79.             ELSE
  80.                add_tmp_file (s)
  81.                add_tmp_file (' is not on any visible lists at this site.\n')
  82.             ENDIF
  83.          ENDIF
  84.       ELSEIF (StrCmp (str, 'WHO', 3))
  85.          add_tmp_file ('\nResponse to command\n>>')
  86.          add_tmp_file (str)
  87.  
  88.          StrCopy (s, from)
  89.          IF (InStr (s, '@') = -1) AND (InStr (s, '!') = -1)
  90.             /* localhost */
  91.             StrAdd (s, '@')
  92.             StrAdd (s, hostname)
  93.          ENDIF
  94.  
  95.          IF (str[4] < 33)     /* No list */
  96.             tmp := list
  97.          ELSE
  98.             s2 := String (StrLen (str)-5)
  99.             StrCopy (s2, str+4)
  100.             tmp := find_list (s2)
  101.             DisposeLink (s2)
  102.          ENDIF
  103.          IF (tmp = NIL)
  104.             add_tmp_file ('Requested list not found.\n')
  105.             JUMP do_next_cmd
  106.          ENDIF
  107.  
  108.          IF (tmp.flags AND CNF_MOD_WHO_SELF)
  109.             IF (on_list (s, tmp.users, (tmp.flags AND CNF_CMP_DOMAIN)) = FALSE)
  110.                add_tmp_file ('Only members of that list may obtain a list of subscribed users.\n')
  111.                JUMP do_next_cmd
  112.             ENDIF
  113.          ELSEIF (tmp.flags AND CNF_MOD_WHO_OWN)
  114.             s2 := String (80)
  115.             StrCopy (s2, tmp.owner)
  116.             IF (tmp.flags AND CNF_CMP_DOMAIN)
  117.                domain_only (s2)
  118.                domain_only (s)
  119.             ENDIF
  120.             IF (StrCmp (s, s2) = FALSE)
  121.                add_tmp_file ('Only the owner of that list may obtain a list of subscribed users.\n')
  122.                DisposeLink (s2)
  123.                JUMP do_next_cmd
  124.             ENDIF
  125.             DisposeLink (s2)
  126.          ENDIF
  127.  
  128.          s2 := tmp.users
  129.          add_tmp_file ('Members of list "')
  130.          add_tmp_file (tmp.name)
  131.          add_tmp_file ('":')
  132.          WHILE (s2)
  133.             add_tmp_file ('\n\t')
  134.             add_tmp_file (s2)
  135.             s2 := Next (s2)
  136.          ENDWHILE
  137.          add_tmp_file ('\n')
  138.       ELSEIF (StrCmp (str, 'ADD', 3))
  139.          add_tmp_file ('\nResponse to command\n>>')
  140.          add_tmp_file (str)
  141.  
  142.          StrCopy (s, from)
  143.          s2 := String (16)
  144.          IF (list)  THEN StrCopy (s2, list.name)
  145.          get_cmd_args (str+4, s2, s)
  146.          add_user (s, s2)
  147.          DisposeLink (s2)
  148.       ELSEIF (StrCmp (str, 'DELETE', 6))
  149.          add_tmp_file ('\nResponse to command\n>>')
  150.          add_tmp_file (str)
  151.  
  152.          StrCopy (s, from)
  153.          s2 := String (16)
  154.          IF (list)  THEN StrCopy (s2, list.name)
  155.          get_cmd_args (str+7, s2, s)
  156.          remove_user (s, s2)
  157.          DisposeLink (s2)
  158.       ELSEIF (StrCmp (str, 'SUBSCRIBE', 9))
  159.          add_tmp_file ('\nResponse to command\n>>')
  160.          add_tmp_file (str)
  161.  
  162.          StrCopy (s, from)
  163.          s2 := String (16)
  164.          IF (list)  THEN StrCopy (s2, list.name)
  165.          get_cmd_args (str+10, s2, s)
  166.          add_user (s, s2)
  167.          DisposeLink (s2)
  168.       ELSEIF (StrCmp (str, 'UNSUBSCRIBE', 11))
  169.          add_tmp_file ('\nResponse to command\n>>')
  170.          add_tmp_file (str)
  171.  
  172.          StrCopy (s, from)
  173.          s2 := String (16)
  174.          IF (list)  THEN StrCopy (s2, list.name)
  175.          get_cmd_args (str+12, s2, s)
  176.          remove_user (s, s2)
  177.          DisposeLink (s2)
  178.       ELSE
  179.          IF (str[0] > 32)
  180.              add_tmp_file ('\nUnknown command:  ')
  181.              add_tmp_file (str)
  182.          ENDIF
  183.       ENDIF
  184. do_next_cmd:
  185.    ENDWHILE
  186.  
  187.    DisposeLink (s)
  188.    add_tmp_file ('\n')
  189.    s := String (70 + EstrLen (from))
  190.    StringF (s, '-f AList -R "AList Mailing List Server" -s "Re: Your command(s)" -t "\s"', from)
  191.    send_message (s)
  192.    DisposeLink (s)
  193. ENDPROC
  194.  
  195.  
  196. /*
  197.  * Checks to see if a string contains anything
  198.  */
  199. PROC is_empty (str)
  200.    IF (str = NIL)  THEN RETURN TRUE
  201.    str := TrimStr (str)
  202.    IF (str[0])  THEN RETURN FALSE  ELSE RETURN TRUE
  203. ENDPROC
  204.  
  205.  
  206. /*
  207.  * Returns the address of the first whitespace (<33) in the string, or NIL
  208.  */
  209. PROC find_empty (str:PTR TO CHAR)
  210.    DEF x
  211.  
  212.    IF (str = NIL)  THEN RETURN NIL
  213.    x := 0
  214.    WHILE (str[x] > 32)
  215.       INC x
  216.    ENDWHILE
  217.  
  218.    IF (str[x])  THEN RETURN str+x
  219. ENDPROC NIL
  220.  
  221.  
  222. /*
  223.  * Adds a user to a list.
  224.  */
  225. PROC add_user (name:PTR TO CHAR, listname:PTR TO CHAR)
  226.    DEF s:PTR TO CHAR, list:PTR TO config_node
  227.  
  228.    IF (listname[0] = 0)
  229.       add_tmp_file ('Unknown list "')
  230.       add_tmp_file (listname)
  231.       add_tmp_file ('".\n')
  232.       RETURN
  233.    ENDIF
  234.  
  235.    list := find_list (listname)
  236.    IF (list = NIL)
  237.       add_tmp_file ('Unknown list "')
  238.       add_tmp_file (listname)
  239.       add_tmp_file ('".\n')
  240.       RETURN
  241.    ENDIF
  242.  
  243. /* Do all tests for permissions to add this user here */
  244.  
  245.    IF (on_list (name, list.users, (list.flags AND CNF_CMP_DOMAIN)))
  246.       add_tmp_file ('User "')
  247.       add_tmp_file (name)
  248.       add_tmp_file ('" is already subscribed to that list.\n')
  249.       RETURN
  250.    ENDIF
  251.  
  252.    s := String (EstrLen (name))
  253.    StrCopy (s, name)
  254.    list.users := add_link (list.users, s)
  255.    add_tmp_file ('User "')
  256.    add_tmp_file (s)
  257.    add_tmp_file ('" has been added to the list "')
  258.    add_tmp_file (list.name)
  259.    add_tmp_file ('".\n')
  260. /* Should really add a post to the owner as well here */
  261.  
  262. /* Now, write out the file */
  263.    IF (write_user_list (list) = FALSE)
  264.       add_tmp_file ('WARNING:  Unable to write user list!  Inform the owner immediately!\n')
  265.    ENDIF
  266. ENDPROC
  267.  
  268.  
  269. /*
  270.  * Removes a user from a list
  271.  */
  272. PROC remove_user (name:PTR TO CHAR, listname:PTR TO CHAR)
  273.    DEF s:PTR TO CHAR, list:PTR TO config_node, s2:PTR TO CHAR
  274.  
  275.    IF (listname[0] = 0)
  276.       add_tmp_file ('Unknown list "')
  277.       add_tmp_file (listname)
  278.       add_tmp_file ('".\n')
  279.       RETURN
  280.    ENDIF
  281.  
  282.    list := find_list (listname)
  283.    IF (list = NIL)
  284.       add_tmp_file ('Unknown list "')
  285.       add_tmp_file (listname)
  286.       add_tmp_file ('".\n')
  287.       RETURN
  288.    ENDIF
  289.  
  290. /* Do all tests for permissions to remove this user here */
  291.  
  292.    IF ((s := on_list (name, list.users, (list.flags AND CNF_CMP_DOMAIN))) = NIL)
  293.       add_tmp_file ('User "')
  294.       add_tmp_file (name)
  295.       add_tmp_file ('" is not subscribed to that list.\n')
  296.       RETURN
  297.    ENDIF
  298.  
  299.    s2 := list.users
  300.    IF (s2 = s)       /* Whoops, removing the head, special case... */
  301.       list.users := Next(s)
  302.       Link (s, NIL)
  303.       DisposeLink (s)
  304.    ELSE
  305.       /* s will still be on from the users list, so no need for NIL checking */
  306.       WHILE (Next (s2) <> s)     /* Note that we don't need to StrCmp here */
  307.          s2 := Next (s2)
  308.       ENDWHILE
  309.       /* Now, we have the one before it */
  310.       Link (s2, Next(s))
  311.       Link (s, NIL)
  312.       DisposeLink (s)
  313.    ENDIF
  314.  
  315.    add_tmp_file ('User "')
  316.    add_tmp_file (s)
  317.    add_tmp_file ('" has been removed from the list "')
  318.    add_tmp_file (list.name)
  319.    add_tmp_file ('".\n')
  320. /* Should really add a post to the owner as well here */
  321.  
  322. /* Now write out the user list */
  323.    IF (write_user_list (list) = FALSE)
  324.       add_tmp_file ('WARNING:  Unable to write user list!  Inform the owner immediately!\n')
  325.    ENDIF
  326. ENDPROC
  327.  
  328.  
  329. /*
  330.  * Returns 2 args, or the defaults
  331.  *
  332.  * Requires def1 and def2 to be valid buffers (e strings).
  333.  */
  334. PROC get_cmd_args (str, def1:PTR TO CHAR, def2:PTR TO CHAR)
  335.    DEF x, y
  336.  
  337.    x := 0
  338.    WHILE (str[x] < 33) AND (str[x] > 0)
  339.       INC x
  340.    ENDWHILE
  341.    y := x
  342.    WHILE (str[x] > 32)
  343.       INC x
  344.    ENDWHILE
  345.    IF (x > y)
  346.       StrCopy (def1, str+y, x-y)
  347.       WHILE (str[x] < 33) AND (str[x] > 0)
  348.          INC x
  349.       ENDWHILE
  350.       y := x
  351.       WHILE (str[x] > 32)
  352.          INC x
  353.       ENDWHILE
  354.       IF (x > y)
  355.          StrCopy (def2, str+y, x-y)
  356.       ENDIF
  357.    ENDIF
  358. ENDPROC
  359.  
  360.  
  361.